package com.unipay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unipay.model.system.SysUser;
import com.unipay.model.vo.RouterVo;
import com.unipay.model.vo.SysUserQueryVo;
import com.unipay.system.mapper.SysUserMapper;
import com.unipay.system.service.SysMenuService;
import com.unipay.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author cmy
 * @since 2023-02-07
 */
@Transactional
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    // 分页条件查询
    @Override
    public IPage<SysUser> selPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        IPage<SysUser> pageModel = baseMapper.selPage(pageParam, sysUserQueryVo);
        return pageModel;
    }
    // 更新用户状态
    @Override
    public int updateStatus(Long id, Integer status) {
        // 根据ID查询
        SysUser sysUser = baseMapper.selectById(id);
        // 更新状态
        sysUser.setStatus(status);
        // 调用方法修改
        int i = baseMapper.updateById(sysUser);
        return i;
        //--------用不到mapper层？？？？？？--------
    }

    // 根据username查询数据
    @Override
    public SysUser getInfoByUsername(String username) {
        SysUser sysUser = baseMapper.selectOne(new QueryWrapper<SysUser>().eq("username", username));
        return sysUser;
    }
    @Autowired
    private SysMenuService sysMenuService;
    //根据用户名称，获取用户信息：基本信息、菜单权限、按钮权限
    @Override
    public Map<String, Object> getUserInfo(String username) {
        HashMap<String, Object> map = new HashMap<>();
        // TODO ---this---
        SysUser sysUser = this.getInfoByUsername(username);
        // TODO （1）根据用户id获取，菜单权限值
        List<RouterVo> routerVoList = sysMenuService.findUserMenuList(sysUser.getId());
        // TODO （2）根据用户id获取，用户按钮权限
        List<String> permsList = sysMenuService.findUserPermsList(sysUser.getId());

        System.out.println("username: " + username);
        System.out.println("sysUser.getName(): "+ sysUser.getName());
        map.put("name", sysUser.getUsername());
        map.put("roles", "[\"admin\"]");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("routers", routerVoList);
        map.put("buttons", permsList);
        return map;
    }
}
